package com.bdqn.zhao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.zhao.dao.IpBuildingMapper;
import com.bdqn.zhao.pojo.IpBuilding;
import com.bdqn.zhao.pojo.SysUser;
import com.bdqn.zhao.service.IpBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 楼宇表 服务实现类
 * </p>
 *
 * @author zhangsan
 * @since 2023-04-24
 */
@Service
public class IpBuildingServiceImpl extends ServiceImpl<IpBuildingMapper, IpBuilding> implements IpBuildingService {

    @Autowired
    private IpBuildingMapper ipBuildingMapper;

    LocalDate date = LocalDate.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    // 实现楼宇信息展示   多表连接查询
    //全查询园区表信息
    @Override
    public List<IpBuilding> selectAllipBuilding(HttpSession session) {

        // 1。全查类别
        Page<IpBuilding> page = new Page<>(1, 3);//设置分页参数
        IPage<IpBuilding> buildingIPage = ipBuildingMapper.selectPage(page, new QueryWrapper<>());
        List<IpBuilding> buildings = buildingIPage.getRecords();//查询到的分页数据
        session.setAttribute("ipParkList", buildings);
        session.setAttribute("ipParkCount", buildings.size());

        //当前页数
        session.setAttribute("current", page.getCurrent());
        session.setAttribute("pages", page.getPages());
        return buildings;
    }


    @Override
    public Boolean ipBuildingsave(int parkId, IpBuilding ipBuilding, HttpSession session) {
        return save(ipBuilding.setParkId(parkId).setRemark("新增楼宇").setCreateBy("栋栋").setCreateTime(Date.valueOf(date.format(formatter))));
    }

    @Override
    public String updates(int buildId, HttpSession session) {
        session.setAttribute("ipPark", getById(buildId));
        return "/ipBuilding";
    }

    @Override
    public Map<String, Object> updates(int buildId, IpBuilding ipBuilding, HttpSession session) {
        HashMap<String, Object> map = new HashMap();
        SysUser sysUser = (SysUser) session.getAttribute("User");
        ipBuilding.setUpdateBy("dong");
        ipBuilding.setUpdateTime(Date.valueOf(date.format(formatter)));
        UpdateWrapper<IpBuilding> updateWrapper2 = new UpdateWrapper();
        updateWrapper2.eq("build_id", buildId);
        map.put("success", update(ipBuilding,updateWrapper2));
        return map;
    }

    @Override
    public Boolean deleteIpBuilding(int buildId) {
        return removeById(buildId);
    }

    @Override
    public Map<String, Object> selectOneipBuild(int index, int count, String buildName) {
        Map<String, Object> map = new HashMap<>();

        List<IpBuilding> ipBuildings = new ArrayList<>();
        Page<IpBuilding> page = new Page<>(index, count);//设置分页参数
        QueryWrapper<IpBuilding> ipParkQueryWrapper = new QueryWrapper<>();
        if (buildName != "" && buildName != null) {
            ipParkQueryWrapper.like("build_name", buildName);
            ipBuildings = ipBuildingMapper.selectList(ipParkQueryWrapper);
        }
        IPage<IpBuilding> ipParkIPage = ipBuildingMapper.selectPage(page, ipParkQueryWrapper);
        List<IpBuilding> ipBuildings1 = ipParkIPage.getRecords();//查询到的分页数据

        map.put("ipParkList", ipBuildings1);
        map.put("ipParkCount", ipBuildings.size());  // 赋值总共的记录数

        map.put("current", page.getCurrent());  //当前页数
        map.put("pages", page.getPages()); // 总页数
        return map;
    }


}
